package com.lepu.app.fun.mother_circle.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class BeanMotherMessage {

    private static final String TABLE_NAME = "MyMessage";

    public String MessageID;
    public String UserID;
    public String Avatar;
    public String UserNickName;
    public String ReplyContent;  //回复内容
    public String ReplyTime;
    public String ReplyType;  //回复类型
    public String TopicID;
    public String CategoryID;  //主题分类
    public String TopicType;
    public String TopicTitle;  //主题标题
    public String TopicDescription;  //主题描述
    public String MediaUri;  //媒体资源
    public String TargetUserID;//	用户ID
    public String TargetUserNickName;// 	昵称
    public String QuoteReplyContent;//	内容TargetUserID	用户ID

    //grow
    public String BabyBirthDate;
    public String AlbumID;
    public String AlbumName;
    public String AlbumDescription;
    public String AlbumCover;
    public String CurrentTime;

    //db
    public int Id;//数据库主键Id
    public int Board;//0 妈妈圈，1成长喜悦
    public int IsRead;//是否已读。0-未读，1-已读
    public String LoginUserID;//用户自己的id

    public static ArrayList<BeanMotherMessage> parseJsonData(String dataString) {
        String result = "";
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray dataArray = obj1.getJSONArray("ListInfo");

            result = dataArray.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        ArrayList<BeanMotherMessage> testArraylist = gson.fromJson(result,
                new TypeToken<List<BeanMotherMessage>>() {
                }.getType());
        return testArraylist;
    }


    public static ArrayList<BeanMotherMessage> getDataList(JSONObject obj1) throws JSONException {
        ArrayList<BeanMotherMessage> dataList = null;
        JSONArray dataArray = obj1.optJSONArray("ListInfo");
        if (dataArray != null && dataArray.length() > 0) {
            dataList = new ArrayList<BeanMotherMessage>();
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject json = (JSONObject) dataArray.get(i);
                BeanMotherMessage message = new BeanMotherMessage();
                message.MessageID = json.optString("IMID");
                message.TopicTitle = json.optString("MessageTitle");
                message.TopicDescription = json.optString("MessageContent");
                message.ReplyTime = json.optString("MessageTime");
                dataList.add(message);
            }

        }
        return dataList;
    }

    //==============================================================
    public static boolean insertMyMessage(BeanMotherMessage item) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("MessageID", item.MessageID);
        values.put("UserID", item.UserID);
        values.put("Avatar", item.Avatar);
        values.put("UserNickName", item.UserNickName);
        values.put("ReplyContent", item.ReplyContent);
        values.put("ReplyTime", item.ReplyTime);
        values.put("ReplyType", item.ReplyType);
        values.put("TopicID", item.TopicID);
        values.put("CategoryID", item.CategoryID);
        values.put("TopicType", item.TopicType);
        values.put("TopicTitle", item.TopicTitle);
        values.put("TopicDescription", item.TopicDescription);
        values.put("MediaUri", item.MediaUri);
        values.put("TargetUserID", item.TargetUserID);
        values.put("TargetUserNickName", item.TargetUserNickName);
        values.put("QuoteReplyContent", item.QuoteReplyContent);

        //grow
        values.put("BabyBirthDate", item.BabyBirthDate);
        values.put("AlbumID", item.AlbumID);
        values.put("AlbumName", item.AlbumName);
        values.put("AlbumDescription", item.AlbumDescription);
        values.put("AlbumCover", item.AlbumCover);

        //db
        values.put("Board", item.Board);
        values.put("IsRead", item.IsRead);
        values.put("LoginUserID", item.LoginUserID);

        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }

    public static boolean deleteItem(String LoginUserID, int id) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "LoginUserID=? and Id=?",
                new String[]{LoginUserID, String.valueOf(id)});

        return flag;
    }

    public static boolean deleteAllItem(String LoginUserID, int board) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "LoginUserID=? and Board=?",
                new String[]{LoginUserID, String.valueOf(board)});

        return flag;
    }

    public static ArrayList<BeanMotherMessage> getMyMessage(String LoginUserID, int board) {
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return null;

        String sql = "select * from MyMessage where LoginUserID=? and Board=? order by ReplyTime desc";
        ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
                new String[]{LoginUserID, String.valueOf(board)});

        if (queryResult == null || queryResult.size() == 0) {
            return null;
        }

        ArrayList<BeanMotherMessage> dataList = new ArrayList<BeanMotherMessage>();
        for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
            HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
            BeanMotherMessage item = BeanMotherMessage
                    .parseFromDatabase(dictionary);
            dataList.add(item);
        }
        return dataList;
    }

    /*
     * 当前用户未读数据
     */
    public static int getNewMessageCount(String LoginUserID, int board) {

        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return count;
        String sql = "select count(*) from MyMessage where LoginUserID=? and Board=? and IsRead=0";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(board)});
        count = UtilityBase.parseInt(result);

        return count;
    }

    public static boolean updateNewMessageRead(String LoginUserID, int board) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("IsRead", 1);

        flag = dbHelp
                .update(TABLE_NAME, values, "LoginUserID=? and Board=?", new String[]{
                        LoginUserID, String.valueOf(board)});

        return flag;
    }


    private static BeanMotherMessage parseFromDatabase(
            HashMap<String, Object> dictionary) {

        if (dictionary == null || dictionary.size() == 0) {
            return null;
        }
        BeanMotherMessage item = new BeanMotherMessage();

        item.Id = UtilityBase.parseInt((String) dictionary.get("Id"));
        item.MessageID = UtilityBase.parseString((String) dictionary.get("MessageID"));
        item.UserID = UtilityBase.parseString((String) dictionary.get("UserID"));
        item.Avatar = UtilityBase.parseString((String) dictionary.get("Avatar"));
        item.UserNickName = UtilityBase.parseString((String) dictionary.get("UserNickName"));
        item.ReplyContent = UtilityBase.parseString((String) dictionary.get("ReplyContent"));
        item.ReplyTime = UtilityBase.parseString((String) dictionary.get("ReplyTime"));
        item.ReplyType = UtilityBase.parseString((String) dictionary.get("ReplyType"));
        item.TopicID = UtilityBase.parseString((String) dictionary.get("TopicID"));
        item.CategoryID = UtilityBase.parseString((String) dictionary.get("CategoryID"));
        item.TopicType = UtilityBase.parseString((String) dictionary.get("TopicType"));
        item.TopicTitle = UtilityBase.parseString((String) dictionary.get("TopicTitle"));
        item.TopicDescription = UtilityBase.parseString((String) dictionary.get("TopicDescription"));
        item.MediaUri = UtilityBase.parseString((String) dictionary.get("MediaUri"));
        item.TargetUserID = UtilityBase.parseString((String) dictionary.get("TargetUserID"));
        item.TargetUserNickName = UtilityBase.parseString((String) dictionary.get("TargetUserNickName"));
        item.QuoteReplyContent = UtilityBase.parseString((String) dictionary.get("QuoteReplyContent"));

        item.BabyBirthDate = UtilityBase.parseString((String) dictionary.get("BabyBirthDate"));
        item.AlbumID = UtilityBase.parseString((String) dictionary.get("AlbumID"));
        item.AlbumName = UtilityBase.parseString((String) dictionary.get("AlbumName"));
        item.AlbumDescription = UtilityBase.parseString((String) dictionary.get("AlbumDescription"));
        item.AlbumCover = UtilityBase.parseString((String) dictionary.get("AlbumCover"));

        item.Board = UtilityBase.parseInt((String) dictionary.get("Board"));
        item.IsRead = UtilityBase.parseInt((String) dictionary.get("IsRead"));
        item.LoginUserID = UtilityBase.parseString((String) dictionary.get("LoginUserID"));

        return item;
    }
}
